Method of pattern searching

ABSTRACT

Structural join mechanisms provide efficient query pattern matching. In one embodiment, tree-merge mechanisms are provided. In another embodiment, stack-tree mechanisms are provided.

CROSS REFERENCE TO RELATED APPLICATIONS

The present application claims the benefit of U.S. Provisional PatentApplication No. 60/450,222, filed on Feb. 25, 2003, which isincorporated herein by reference.

STATEMENT REGARDING FEDERALLY SPONSORED RESEARCH

The Government may have certain rights in the invention pursuant to aNational Science Foundation grant under Grant Numbers IIS-9986030 andIIS-0208852.

FIELD OF THE INVENTION

The present invention relates generally to processing queries in acomputer system and, more particularly, to processing computer queriesusing pattern matching.

BACKGROUND OF THE INVENTION

As is known in the art, the eXtensible Markup Language (XML) employs atree-structured model for representing data. Queries in XML querylanguages typically specify patterns of selection predicates on multipleelements that have some specified tree structured relationships. Forexample, the XQuery path expression:

book[title=‘XML’]//author[.=‘jane’]

matches author elements that (i) have as content the string value“jane”, and (ii) are descendants of book elements that have a childtitle element whose content is the string value “XML”.

This XQuery path expression can be represented as a node-labeled treepattern with elements and string values as node labels. Such a complexquery tree pattern can be decomposed into a set of basic parent-childand ancestor-descendant relationships between pairs of nodes. Forexample, the basic structural relationships corresponding to the abovequery are the ancestor-descendant relationship (book, author) and theparent-child relationships (book, title), (title, XML) and (author,jane). The query pattern can then be matched by (i) matching each of thebinary structural relationships against the XML database, and (ii)“stitching” together these basic matches. Finding all occurrences ofthese basic structural relationships in an XML database is a coreoperation in XML query processing, both in relational implementations ofXML databases, and in native XML databases.

There have been various attempts determine how to find occurrences ofsuch structural relationships (as well as the query tree patterns inwhich they are embedded) using relational database systems, as well asusing native XML query engines. These works typically use somecombination of indexes on elements and string values, tree traversalalgorithms, and join algorithms on the edge relationships between nodesin the XML data tree.

One known attempt is described in C. Zhang, J. Naughton, D. Dewitt, Q.Luo, and G. Lohman, “On supporting containment queries in relationaldatabase management systems,” Proceedings of SIGMOD, 2001, hereinafter“Zhang”), which is incorporated herein by reference. Zhang proposes avariation of the traditional merge join algorithm, called themulti-predicate merge join (MPMGJN) algorithm, for finding alloccurrences of the basic structural relationships (referred to ascontainment queries). Zhang compared the implementation of containmentqueries using native support in two commercial database systems, and aspecial purpose inverted list engine based on the MPMGJN algorithm. Theresults in Zhang showed that the MPMGJN algorithm could outperformstandard Relational Database Management System (RDBMS) join algorithmsby more than an order of magnitude on containment queries. The key tothe efficiency of the MPMGJN algorithm is the “(DocId, StartPos:EndPos,LevelNum) representation of positions of XML elements, and the “(DocId,StartPos, LevelNum)” representation of positions of string values, thatsuccinctly capture the structural relationships between elements (andstring values) in the XML database. Checking that structuralrelationships in the XML tree, like ancestor-descendant and parent-child(corresponding to containment and direct containment relationships,respectively, in the XML document representation), are present betweenelements amounts to checking that certain inequality conditions holdbetween the components of the positions of these elements.

While the MPMGJN algorithm outperforms standard RDBMS join algorithms, asignificant amount of unnecessary computation and I/O operations areperformed for matching basic structural relationships, especially in thecase of parent-child relationships (or, direct containment queries).

It would, therefore, be desirable to overcome the aforesaid and otherdisadvantages.

SUMMARY OF THE INVENTION

The present invention provides a system and method for efficient querypattern matching. The inventive join methods match structuralrelationships against a database, such as an XML database. While theinvention is primarily shown and described in conjunction with XML querypattern matching, it is understood that the invention is applicable to avariety of database types having structural relationships.

In one aspect of the invention, tree-merge query processing is provided.In another aspect of the invention, stack-tree query processing isprovided. The tree-merge and stack tree processing provides efficientquery pattern matching in XML databases, for example.

BRIEF DESCRIPTION OF THE DRAWINGS

The invention will be more fully understood from the following detaileddescription taken in conjunction with the accompanying drawings, inwhich:

FIG. 1A is a pictorial representation of an exemplary XML documentfragment that can be processed in accordance with the present invention;

FIG. 1B is a tree representation of a the XML document fragment of FIG.1A;

FIG. 2A is a pictorial representation of an exemplary tree pattern thatcan be processed in accordance with the present invention;

FIG. 2B is a pictorial representation of the structural relationships ofthe tree pattern of FIG. 2A;

FIG. 3 is a textual representation of an exemplary Tree-Merge-Ancalgorithm implementation in accordance with the present invention;

FIG. 3A is a flow diagram showing an exemplary sequence of steps forimplementing the algorithm of FIG. 3;

FIG. 4 is a textual representation of an exemplary Tree-Merge-Descalgorithm implementation in accordance with the present invention;

FIG. 4 A is a flow diagram showing an exemplary sequence of steps forimplementing the algorithm of FIG. 4;

FIGS. 5A and 5B are pictorial representations of worst case scenariosfor the Tree-Merge-Anc algorithm of FIG. 3;

FIGS. 5C and 5D are pictorial representations of worst case scenariosfor the Tree-Merge-Desc algorithm of FIG. 4;

FIG. 6 is a textual representation of an exemplary Stack-TreeDescalgorithm implementation in accordance with the present invention;

FIG. 6A is a flow diagram showing an exemplary sequence of steps forimplementing the algorithm of FIG. 6;

FIG. 7A is an exemplary dataset that can be processed by theStack-Tree-Desc algorithm of FIG. 6;

FIGS. 7B-7E show respective processing steps during evaluation of theStack-Tree-Desc algorithm of FIG. 6;

FIG. 8 is a textual representation of an exemplary Stack-Tree-Ancalgorithm in accordance with the present invention;

FIG. 8A is a flow diagram showing an exemplary sequence of steps forimplementing the algorithm of FIG. 8;

FIG. 9 is a textual representation of an exemplary DTD that can be usedin an exemplary implementation of the invention;

FIG. 9A is a textual representation of characteristics of the dataset ofFIG. 9;

FIG. 9B is a textual representation of queries that can be used in anexemplary implementation of the invention;

FIG. 10 is a graphical depiction of the performance of algorithms inaccordance with the present invention in an illustrative implementation;and

FIG. 11 is a further graphical depiction of the performance ofalgorithms in accordance with the present invention in an illustrativeimplementation;

DETAILED DESCRIPTION OF THE INVENTION

XML queries typically specify patterns of selection predicates onmultiple elements that have some specified tree-structuredrelationships. The primitive tree-structured relationships areparent-child and ancestor-descendant, and finding occurrences of theserelationships in an XML database is a core operation for XML queryprocessing.

Before describing the invention in detail, some basic XML concepts setforth. An XML database is a forest of rooted, ordered, labeled trees,with each node corresponding to an element and the edges representing(direct) element-subelement relationships. Node labels include a set of(attribute, value) pairs, which suffices to model tags, ParsibleCharacter Data (PCDATA) content, etc.

FIG. 1A shows an illustrative XML document for which a treerepresentation is shown in FIG. 1B. The utility of the numbersassociated with the tree nodes will be described below. Queries in XMLquery languages, such as XQuery, Quilt, and XML-QL, use (node labeled)tree patterns for matching relevant portions of data in the XMLdatabase. The query pattern node labels include element tags,attribute-value comparisons, and string values, and the query patternedges are either parent-child edges (depicted using single line) orancestor-descendant edges (depicted using a double line). For example,the XQuery path expression in the introduction can be represented as therooted tree pattern in FIG. 2A. This query pattern matches the documentin FIG. 1A.

In general, at each node in the query tree pattern, there is a nodepredicate that specifies some predicate on the attributes (e.g., tag,content) of the node in question. It is understood that what ispermitted in this predicate is not material. It suffices that there bethe possibility of constructing efficient access mechanisms (such asindex structures) to identify the nodes in the XML database that satisfyany given node predicate.

A complex query tree pattern can be decomposed into a set of basicbinary structural relationships, such as parent-child andancestor-descendant between pairs of nodes. The query pattern can thenbe matched by (i) matching each of the binary structural relationshipsagainst the XML database, and (ii) “stitching” together these basicmatches. For example, the basic structural relationships correspondingto the query tree pattern of FIG. 2A are shown in FIG. 2B.

One conventional approach to matching structural relationships againstan XML database is to use traversal-style algorithms by usingchild-pointers or parent-pointers. Such “tuple-at-a-time” processingstrategies are known to be relatively inefficient compared to theset-at-a-time strategies used in database systems. Pointer-based joinshave been shown to be relatively efficient in object-oriented databases.

In the context of XML databases, nodes may have a large number ofchildren, and the query pattern often requires matchingancestor-descendant structural relationships (for example, the (book,author) edge in the query pattern of FIG. 2A, in addition toparent-child structural relationships. In this case, there are twooptions: (i) explicitly maintaining only (parent, child) node pairs andidentifying (ancestor, descendant) node pairs through repeated joins; or(ii) explicitly maintaining (ancestor, descendant) node pairs. Theformer approach may require excessive query processing time, while thelatter approach would use excessive (quadratic) space. In either case,using pointer-based joins is likely to be infeasible.

One factor in generating an efficient, uniform mechanism forset-at-a-time (join-based) matching of structural relationships is apositional representation of occurrences of XML elements and stringvalues in the XML database, which extends the classic inverted indexdata structure in information retrieval as is well known in the art. Theposition of an element occurrence in the XML database can be representedas the 3-tuple (DocId, StartPos:EndPos, LevelNum), and the position of astring occurrence in the XML database can be represented as the 3-tuple(DocId, StartPos, LevelNum), where (i) DocId is the identifier of thedocument; (ii) StartPos and EndPos can be generated by counting wordnumbers from the beginning of the document with identifier DocId untilthe start of the element and end of the element, respectively; and (iii)LevelNum is the nesting depth of the element (or string value) in thedocument. FIG. 1B, for example, depicts a 3-tuple with each tree node,based on this representation of position. Note that the DocId for eachof these nodes is chosen to be one.

Structural relationships between tree nodes (elements or string values)whose positions are recorded in this fashion can be determinedrelatively easily: (i) ancestor-descendant: a tree node n₂ whoseposition in the XML database is encoded as (D₂, S₂: E₂, L₂) is adescendant of a tree node n₁ whose position is encoded as (D₁, S₁: E₁,L₁) iff (if and only if) D₁=D₂, S₁<S₂ and E₂<E₁; (ii) parent-child: atree node n₂ whose position in the XML database is encoded as (D₂, S₂:E₂, L₂) is a child of a tree node n₁ whose position is encoded as (D₁,S₁: E₁, L₁) iff D₁=D₂, S₁<S₂, E₂<E₁, and L₁+1=L₂. It is understood thatthe following shorthand notation is used above: D=DocId, S=StartPos,E=EndPos, and L=LevelNum.

For example, in FIG. 1B, the author node with position (1, 6:8, 3) is adescendant of the book node with position (1, 1:70, 1), and the string“jane” with position (1, 7, 4) is a child of the author node withposition (1, 6:8, 3).

It should be noted that in this representation of node positions in theXML data tree checking an ancestor-descendant structural relationship isas easy as checking a parent-child structural relationship. The reasonis that one can check for an ancestor-descendant structural relationshipwithout knowledge of the intermediate nodes on the path. It should alsobe noted that this representation of positions of elements and stringvalues allow for checking order and proximity relationships betweenelements and/or string values.

In one aspect of the invention, the (DocId, StartPos:EndPos, LevelNum)representation of positions of XML elements and string values areutilized to achieve novel, I/O and CPU optimal (in an asymptotic sense)join algorithms for matching basic structural relationships (or,containment queries) against an XML database.

In general, the task of matching a relatively complex XML query patternreduces to that of evaluating a join expression with one join operatorfor each binary structural relationship in the query pattern. It isunderstood that different join orderings may result in differentevaluation costs.

Described below are two families of inventive join algorithms formatching parent-child and ancestor-descendant structural relationshipsefficiently: tree-merge and stack-tree algorithms.

Consider an ancestor-descendant (or parent-child) structuralrelationship (e₁, e₂), for example, (book, author) (or (author, jane))in the running example. Let AList=[a₁, a₂, . . . ] and DList=[d₁, d₂, .. . ] be the lists of tree nodes that match the node predicates e₁ ande₂ respectively, each list sorted by the (DocId, StartPos) values of itselements. There are a number of ways in which the AList and the DListcould be generated from the database that stores the XML data. In oneembodiment, a native XML database system stores each element node in theXML data tree as an object with the attributes: ElementTag, DocId,StartPos, EndPos, and LevelNum. An index can be built across all theelement tags, which can then be used to find the set of nodes that matcha given element tag. The set of nodes can then be sorted by (DocId,StartPos) to produce the lists that serve as input to the inventive joinalgorithms.

Given these two input lists, AList of potential ancestors (or parents)and DList of potential descendants (resp., children), the algorithms ineach family can output a list OutputList=[(a_(i), d_(j))] of joinresults, sorted either by (DocId, a_(i).StartPos, d_(j).StartPos) or by(DocId, d_(j).StartPos, a_(i).StartPos). Both variants are useful, andthe variant chosen may depend on the order in which an optimizer choosesto compose the structural joins to match the complex XML query pattern.

In general, a modified merge-join is performed, possibly by performingmultiple scans through the “inner” join operand to the extent necessary.Either AList or DList can be used as the inner (or outer) operand forthe join: the results are produced sorted (primarily) by the outeroperand.

FIG. 3 shows the tree-merge algorithm for the case when the outer joinoperand is the ancestor. Similarly, FIG. 4 shows the case when the outerjoin operand is the descendant. It is understood that for ease ofunderstanding, both algorithms assume that all nodes in the two listshave the same value of DocId, their primary sort attribute. Dealing withnodes from multiple documents is straightforward, requiring thecomparison of DocId values and the advancement of node pointers as inthe traditional merge join.

FIG. 3A shows an exemplary sequence of steps for the inventivetree-merge-anc algorithm of FIG. 3. It is assumed that all nodes inAlist and Dlist have the same document ID DocID. It is understood thatAlist refers to the list of potential ancestors in sorted order of thestarting position StartPos and Dlist refers to the list of potentialdescendants in sorted order of StartPos.

In step 100, variable a is set to the first node in Alist and in step102 it is determined whether the first node is not a null value. If not,processing terminates. If so (not a null value), in step 104, Dlistnodes d that are unmatchable are skipped over. In step 106, d is set tothe next node in Dlist and it is determined in step 108 whether thecurrent Dlist node d is a not a null value and the end position EndPosof the Dlist node is less than the end position of the current Alistnode. If not, a is set to the next Alist node in step 110 and processingcontinues in step 104. If so, in step 112, it is determined if thecurrent Alist node starting position StartPos is less than the currentDlist node starting position, the Dlist node end position is less thanthe Alist node end position, and the Dlist node level number equals theAlist node level number plus one. If so, the node pair (a,d) is appendedto the output list of join values in step 114. If not, processingcontinues in step 106.

FIG. 4A shows an exemplary sequence of steps for the inventivetree-merge-dec algorithm of FIG. 4. In step 200, variable d is set tothe first node in Dlist and in step 202 it is determined whether thefirst node is not a null value. If not, processing terminates. If so(not a null value), in step 204, Alist nodes that are unmatchable areskipped over. In step 206, a is set to the next node in Alist and it isdetermined in step 208 whether the current Alist node a is a not a nullvalue and the starting position StartPos of the Alist node is less thanthe starting position of the current Dlist node. If not, d is set to thenext Dlist node in step 210 and processing continues in step 204. If so,in step 212, it is determined if the current Alist node startingposition a.StartPos is less than the current Dlist node d.StartPosstarting position, the Dlist node end position is less than the Alistnode end position, and the Dlist node level number equals the Alist nodelevel number plus one. If so, the node pair (a,d) is appended to theoutput list of join values in step 214. If not, processing continues instep 206.

Traditional merge joins that use a single equality condition between twoattributes as the join predicate can be shown to have time and spacecomplexities O(|input|+|output|) on sorted inputs, while producing asorted output. In general, one cannot establish the same time complexitywhen the join predicate involves multiple equality and/or inequalityconditions. In accordance with the present invention, criteria underwhich tree-merge algorithms have asymptotically optimal time complexitycan be identified.

In one aspect of the invention, a Tree-Merge-Anc algorithm forancestor-descendant structural relationship is provided as shown in FIG.3. The space and time complexities of Algorithm Tree-Merge-Anc areO(|AList|+|DList|+OutputList|) for the ancestor-descendant structuralrelationship. Consider first the case where no two nodes in AList arethemselves related by an ancestor-descendant relationship. In this case,the size of OutputList is O(|AList|+|DList|), Algorithm Tree-Merge-Ancmakes a single pass over the input AList and at most two passes over theinput DList.

Consider next the case where multiple nodes in AList are themselvesrelated by an ancestor-descendant relationship. This can happen, forexample, in the (section, head) structural relationship for the XML datain FIG. 1. In this case, multiple passes may be made over the same setof descendant nodes in DList, and the size of OutputList may beO(|AList|*(DList|), which is quadratic in the size of the input lists.However, it can be seen that the algorithm still has optimal timecomplexity, i.e., O(|AList|+(DList|+|OutputList|).

In another aspect of the invention, a Tree-Merge-Anc for parent-childstructural relationships is provided. When evaluating a parent-childstructural relationship, the time complexity of Algorithm Tree-Merge-Ancis the same as if one were performing an ancestor-descendant structuralrelationship match between the same two input lists. However, the sizeof OutputList for the parent-child structural relationship can besignificantly smaller than the size of the OutputList for theancestor-descendant structural relationship. In particular, consider thecase when all the nodes in AList form a (long) chain of length n, andeach node in AList has two children in DList, one on either side of itschild in AList, as shown in FIG. 5A. In this case, it is relatively easyto verify that the size of OutputList is O(|AList|+|DList|), but thetime complexity of Algorithm Tree-Merge-Anc isO((|AList|+|DList|+OutputList|)²). An evaluation of this relationship isshown pictorially FIG. 5B, where each node in AList is associated withthe sublist of DList that needs to be scanned. The I/O complexity isalso quadratic in the input size in this case.

In a further aspect of the invention, a Tree-Merge-Desc algorithm isprovided. The time complexity of the algorithm can beO((|AList|+|DList|+OutputList|)²) in the worst case. This occurs, forexample, in the case shown in FIG. 5C, when the first node in AList isan ancestor of each node in DList. In this case, each node in DList hasonly two ancestors in AList, so the size of OutputList isO(|AList|+|DList|), but AList is repeatedly scanned, resulting in a timecomplexity of O(|AList|*|DList|), the evaluation of which is depicted inFIG. 5D, where each node in DList is associated with the sublist ofAList that needs to be scanned.

In another aspect of the invention, a series of Stack-Tree JoinAlgorithms are provided. It can be seen that a depth-first traversal ofa tree can be performed in linear time using a stack having a size aslarge as the height of the tree. In the course of this traversal, everyancestor-descendant relationship in the tree is manifested by thedescendant node appearing somewhere higher on the stack than theancestor node. This can provide the basis for a family of stack-basedstructural join algorithms, with better worst-case I/O and CPUcomplexity than the tree-merge family, for both parent-child andancestor-descendant structural relationships.

However, the depth-first traversal idea, while appealing at firstglance, cannot be used directly since it requires traversal of theentire database. It would be desirable to traverse only the candidatenodes provided as part of the input lists. The inventive stack-treefamily of structural join algorithms are described below. It is believedthat these algorithms do not have counterparts in traditional joinprocessing.

For the Stack-Tree-Desc algorithm, consider an ancestor-descendantstructural relationship (e₁,e₂). Let AList=[a₁, a₂, . . . ] andDList=[d1, d2, . . . ] be the lists of tree nodes that match nodepredicates e₁ and e₂, respectively, sorted by the (DocId, StartPos)values of its elements.

The stack-tree algorithm for the case when the output list [(a_(i),d_(j))] is sorted by (DocId, d_(j).StartPos, a_(i).StartPos). This isboth simpler to understand and relatively efficient in practice.

FIG. 6 shows the algorithm for the ancestor-descendant case. In general,the algorithm takes the two input operand lists, AList and DList, bothsorted on their (DocId, StartPos) values and conceptually merges(interleave) them. As the merge proceeds, it determines theancestor-descendant relationship, if any, between the current top ofstack and the next node in the merge, i.e., the node with the smallestvalue of StartPos. Based on this comparison, the stack is manipulated,and output produced.

The stack at all times has a sequence of ancestor nodes, each node inthe stack being a descendant of the node below it. When a new node fromthe AList is found to be a descendant of the current top of stack, it issimply pushed on to the stack. When a new node from the DList is foundto be a descendant of the current top of stack, it is known that it is adescendant of all the nodes in the stack. Also, it is guaranteed that itwill not be a descendant of any other node in AList. Hence, the joinresults involving this DList node with each of the AList nodes in thestack are output. If the new node in the merge list is not a descendantof the current top of stack, then it is guaranteed that no future nodein the merge list is a descendant of the current top of stack, so thestack can be popped, and the test repeated with the new top of stack. Nooutput is generated when any element in the stack is popped.

FIG. 6A shows an exemplary sequence of steps for implementing thealgorithm of FIG. 6 with the same assumptions set forth above inconjunction with FIG. 3A. In step 300, variable a is set to the firstnode of Alist and variable d is set to the first node of Dlist. In step302 it is determined whether the input lists are not empty or the stackis not empty. If not (input lists, stack empty), then processingterminates. If so, in step 304 it is determined whether the startingposition of the Alist node a.StartPos is greater than the end positionof the node on the top of the stack and the starting position of theDlist node is greater than the end position of the node on the top ofthe stack. If so, the top element in the stack is popped in step 306 andprocessing continues in step 302. If not, in step 308, it is determinedwhether the start position of the current Alist node is less than thestart position of the Dlist node. If so, in step 310, the Alist node ispushed onto the stack and in step 312, the next Alist node is examinedwith processing continuing in step 302. If not, in step 314 the matches(a1,d) between a1's on the stack are appended to the output list, wherea1 is an index on the bottom stack element. In step 316, the next Dlistnode is examined with processing continuing in step 302.

The parent-child case of Algorithm Stack-Tree-Desc is simpler since aDList node can join only (if at all) with the top node on the stack. Inthis case, the “for loop” inside the “else” case of FIG. 6 is replacedwith:

if (d.LevelNum_stack→top.LevelNum.1) append (stack→top,d) to OutputList

FIG. 8A shows an exemplary sequence of steps for implementing theinventive stack-tree-anc algorithm. As noted above, there is overlapwith the stack-tree-desc algorithm set forth in FIG. 6A and adescription of common steps is not repeated. After popping the stack instep 306, it is determined in step 318 whether the stack is empty. Ifso, in step 320 there is a conventional merge of the tuple's self andinherit lists into the stack's top inherit list and processing continuesin step 302. If not, in step 322 the tuple self and inherit list isoutput.

Example for Algorithm Stack-Tree-Desc

Some steps during an example evaluation of Algorithm Stack-Tree-Desc,for a parent-child structural relationship, on the dataset of FIG. 7Aare shown in FIGS. 7B-7E. The a_(i)'s are the nodes in AList and thed_(j)'s are the nodes in DList. Initially, the stack is empty, and theconceptual merge of AList and DList is shown in FIG. 7B. In FIG. 7C, a₁has been put on the stack, and the first new element of the merged list,d₁, is compared with the stack top (at this point a₁, d₁ is output).FIG. 7D illustrates the state of the execution several steps later, whena₁, a₂, . . . a_(n) are all on the stack, and d_(n) is being comparedwith the stack top (after this point, the OutputList includes a₁, d₁,(a₂,d₂), . . . , (a_(n),d_(n))). Finally, FIG. 7E shows the state of theexecution when the entire input has almost been processed. Only a₁remains on the stack (all the other a_(i)'s have been popped from thestack), and d_(2n) is compared with a₁. Note that all the desiredmatches have been produced while making only a single pass through theentire input. Recall that this is the same dataset of FIG. 5A, whichillustrated the sub-optimality of Algorithm Tree-Merge-Anc, for the caseof parent-child structural relationships.

The stack-tree algorithm for the case when the output list[(a_(i),d_(j))] needs to be sorted by (DocId, a_(i).StartPos,d_(j).StartPos) is now described. It is not straightforward to modifyAlgorithm Stack-Tree-Desc described above to produce results sorted byancestor because of the following: if node a from AList on the stack isfound to be an ancestor of some node d in the DList, then every node a′from AList that is an ancestor of a (and hence below a on the stack) isalso an ancestor of d. Since the StartPos of a′ precedes the startposition of a, the system should delay output of the join pair (a,d)until after (a′,d) has been output. There remains the possibility of anew element d′ after d in the DList joining with a′ as long as a′ is onstack, so the pair (a,d) cannot be output until the ancestor node a′ ispopped from stack. Meanwhile, large join results can be built up thatcannot yet be output.

An exemplary solution is shown in FIG. 8 for the ancestor-descendantcase. As with Algorithm Stack-Tree-Desc, the stack at all times has asequence of ancestor nodes, each node in the stack being a descendant ofthe node below it. Now, one associates two lists with each node on thestack: the first, called self-list, is a list of result elements fromthe join of this node with appropriate DList elements; the second,called inherit-list is a list of join results involving AList elementsthat were descendants of the current node on the stack. As before, whena new node from the AList is found to be a descendant of the current topof stack, it is simply pushed on to the stack. When a new node from theDList is found to be a descendant of the current top of stack, it issimply added to the self-lists of the nodes in the stack. Again, asbefore, if no new node (from either list) is a descendant of the currenttop of stack, then it is guaranteed that no future node in the mergelist is a descendant of the current top of stack, so stack can bepopped, and the test repeated with the new top of stack. When the bottomelement in stack is popped, its self-list is output first and then itsinherit-list. When any other element in stack is popped, no output isgenerated. Instead, its inherit-list is appended to its self-list, andthe result is appended to the inherit-list of the new top of stack.

An optimization to the algorithm (incorporated in FIG. 8) is as follows:no self-list is maintained for the bottom node in the stack. Instead,join results with the bottom of the stack are output immediately. Thisresults in a small space savings, and renders the stack-tree algorithmpartially non-blocking.

The Algorithm Stack-Tree-Desc is relatively straightforward to analyze.Each AList element in the input may be examined multiple times, butthese can be amortized to the element on DList, or the element at thetop of stack, against which it is examined. Each element on the stack ispopped at most once, and when popped, causes examination of the new topof stack with the current new element. Finally, when a DList element iscompared against the top element in stack, then it either joins with allelements on stack or none of them; all join results are immediatelyoutput. In other words, the time required for this part is directlyproportional to the output size. Thus, the time required for thisalgorithm is O(|input|+|output|) in the worst case. Putting all thistogether, it can be seen that the space and time complexities ofAlgorithm Stack-Tree-Desc can be defined asO(|AList|+|DList|+OutputList|), for both ancestor-descendant andparent-child structural relation-ships. Further, AlgorithmStack-Tree-Desc is a non-blocking algorithm. It is believed that noother known join algorithm that has the same input lists, and isrequired to compute the same output list, could have better asymptoticcomplexity.

The I/O complexity analysis is relatively straightforward as well. Eachpage of the input lists is read once, and the result is output as soonas it is computed. Since the maximum size of stack is proportional tothe height of the XML database tree, it seems reasonable to assume thatall of stack fits in memory at all time. Hence, the following result canbe seen: the I/O complexity of Algorithm Stack-Tree-Desc is

$O\left( {\frac{{AList}}{B} + \frac{{DList}}{B} + \frac{{OutputList}}{B}} \right)$for ancestor-descendant and parent-child structural relationships, whereB is the blocking factor, which refers to the size of a disk block.

One difference between the analyses of Algorithms Stack-Tree-Anc andStack-Tree-Desc is that join results are associated with nodes in thestack in Algorithm Stack-Tree-Anc. It can be seen that the list of joinresults at any node in the stack is linear in the output size. Whatremains to be analyzed is the appending of lists each time the stack ispopped.

If the lists are implemented as linked lists (with start and endpointers), these append operations can be carried out in unit time, andrequire no copying. Thus one comparison per AList input and one peroutput are all that are performed to manipulate stack. Combined with theanalysis of Algorithm Stack-Tree-Desc, it can be seen that the timerequired for this algorithm is still O(|input|+|output|) in the worstcase.

The I/O complexity analysis is a somewhat more involved. Certainly, onecannot assume that all the lists of results not yet output fit inmemory. Careful buffer management is required. In one embodiment, theonly operation performed on a list is to append to it (except for thefinal read out). As such, one only needs to have access to the tail ofeach list in memory as computation proceeds. The rest of the list can bepaged out. When list x is appended to list y, it is not necessary thatthe head of list x be in memory, the append operation only establishes alink to this head in the tail of y. So all that is needed is to know thepointer for the head of each list, even if it is paged out. Each listpage is thus paged out at most once, and paged back in again only whenthe list is ready for output. Since the total number of entries in thelists is exactly equal to the number of entries in the output, one hasthe I/O required on account of maintaining lists of results isproportional to the size of output (provided that there is enough memoryto hold in buffer the tail of each list: requiring two pages of memoryper stack entry-still a reasonable requirement). Other I/O activity isfor the input and output. This leads to the desired linearity result.

The space and time complexities of Algorithm Stack-Tree-Anc areO(|AList|+|DList|+OutputList|), for both ancestor-descendant andparent-child structural relation-ships. The I/O complexity of AlgorithmStack-Tree-Anc is

$O\left( {\frac{{AList}}{B} + \frac{{DList}}{B} + \frac{{OutputList}}{B}} \right)$for both ancestor-descendant and parent-child structural relationships,where B is the blocking factor.

Results of an actual implementation of the various join algorithms forXML data sets are described below. In particular, results for thestructural join algorithms namely, TREE-MERGE JOIN(TMJ) and STACK-TREEJOIN (STJ) are provided. Once more, the output can be sorted in twoways, based on the “ancestor” node or the “ancestor” node in the join.Correspondingly, two “flavors” of these algorithms are considered. Thesuffix “-A” (ancestor) and “-D” (descendant) are used to differentiatebetween these. The four algorithms are thus labeled: TMJ-A, TMJ-D, STJ-Aand STJ-D.

The join algorithms were implemented in a TIMBER XML query engine. As isknown in the art, TIMBER is an native XML query engine that is built ontop of a SHORE type object manager. Since the goal of TIMBER is toefficiently handle complex XML queries on large data sets, thealgorithms were implemented so that they could participate in complexquery evaluation plans with pipelining. The experiments using TIMBERwere run on a 500 MHz Intel Pentium III processor running WindowsNTWorkstation v4.0. SHORE was compiled for a 8 KB page size. SHORE bufferpool size was set to 32 MB, and the container size was 8000 bytes. Thenumbers presented here were produced by running the experiments multipletimes and averaging all the execution times except for the first run(i.e., these are warm cache numbers).

For the workload, the IBM XML data generator was used to generate anumber of data sets, of varying sizes and other data characteristics,such as the fanout (MaxRepeats) and the maximum depth, using theOrganization DTD presented in FIG. 9. The so-called XMach-1 and XMarkbenchmarks, were used as well as some real XML data. The resultsobtained were similar in all cases. Results are presented for thelargest organization data set that was generated. This data set consistsof 6.3 million element nodes, corresponding to approximately 800 MB ofXML documents in text format. The characteristics of this data set interms of the number of occurrences of element tags are summarized inFIG. 9A.

The various join algorithms were evaluated using the set of queriesshown in FIG. 9B. The queries are broken up into two classes. QS1 to QS6are simple structural relationship queries, and have an equal mix ofparent-child queries and ancestor-descendant queries. QC1 and QC2 arecomplex chain queries, and are used to demonstrate the performance ofthe algorithms when evaluating complex queries with multiple joins in apipeline.

The focus in the experiments is to characterize the performance of thefour structural join algorithms, and understand their differences.Before doing so, some additional detail regarding the manner in whichthese were implemented for the experiments reported is presented. Thechoice of implementation, i.e., on top of SHORE and TIMBER, was drivenby the need for sufficient control. It is understood that the algorithmsthemselves could just as well have been implemented on many otherplat-forms, including (as new join methods) on relational databases.

The join algorithms were implemented using the so-called operatoriterator model. In this model, each operator provides an open, next andclose interface to other operators, and allows the database engine toconstruct an operator tree with an arbitrary mix of query operations(different join algorithms or algorithms for other operations such asaggregation) and naturally allows for a pipelined operator evaluation.To support this iterator model, attention is paid to the manner in whichresults are passed from one operator to another. Algorithms such as theTMJ algorithms may need to repeatedly scan over one of the inputs. Suchrepeated scans are feasible if the input to a TMJ operator is a streamfrom a disk file, but is not feasible if the input stream originatesfrom another join operator (in the pipeline below it). The TMJalgorithms were implemented so that the nodes in a current sweep arestored in a temporary SHORE file. On the next sweep, this temporarySHORE file is scanned. This allows limitation of the memory used by TMJimplementation, as the only memory used is managed by the SHORE buffermanager, which takes care of evicting pages of the temporary file fromthe buffer pool if required. Similarly for the STJ-A algorithm, theinherit- and self-lists are stored in a temporary SHORE file, againlimiting the memory used by the algorithm. In both cases, theimplementation turns logging and locking off for the temporary SHOREfiles. Note that STJ-D can join the two inputs in a single pass overboth inputs, and, never has to spool any nodes to a temporary file.

To amortize the storage and access overhead associated with each SHOREobject, the nodes are grouped into a large container object, and a SHOREobject is created for each container. The join algorithms write nodes tocontainers and when a container is full it is written to the temporarySHORE file as a SHORE record. The performance benefits of this approachare substantial as will be appreciated by one of ordinary skill in theart.

The performance of the STJ and the TMJ algorithms are compared using allthe six simple queries, QS1-QS6, shown in FIG. 9B. FIG. 10 plots theperformance of the four algorithms. As shown, the STJ-D algorithmoutperforms the remaining algorithms in all cases. It is believed thatthe reason for the superior performance of STJ-D is because of itsability to join the two data sets in a single pass over the input nodes,and it never has to write any nodes to intermediate files on disk.

It can also be seen that STJ-A usually has better performance than bothTMJ-A and TMJ-D. For queries QS4 and QS6, the STJ-A algorithms and thetwo TMJ algorithms have comparable performance. These queries have largeresult sizes (approximately 600K and 1M tuples respectively as shown inFIG. 9A. Since STJ-A keeps the results in the lists associated with thestack, and can output the results only when the bottom-most element ofthe stack is popped, it has to perform many writes and transfers of thelists associated with the stack elements. In the exemplaryimplementation, these lists are maintained in temporary SHORE files.With larger result sizes this list management slows down the performanceof STJ-A in practice. It can also be seen that the two TMJ algorithmshave comparable performance.

These experiments were also ran with reduced buffer sizes and it wasfound that for this data set the execution time of all the algorithmsremained fairly constant. Even though the XML data sets are relativelylarge, after applying the predicates, the candidate lists that arejoined are not very large. Furthermore, the effect of buffer pool sizemay be significant when one of the inputs has nodes that are deeplynested amongst themselves, and the node that is higher up in the XMLtree has many nodes that it joins with.

For example, consider the TMJ-A algorithms, and the query“manager/employee”. If many manager nodes are nested below a managernode that is higher up in the XML tree, then after the join of themanager node at the top is done, repeated scans of the descendant nodeswill be required for the manager nodes that are descendants of themanager node at the top. Such scenarios are rare in the illustrativedata set, and, consequently, the buffer pool size has only a marginalimpact on the performance of the algorithms.

The performance of the algorithms using the two complex chain queries,QC1 and QC2, from FIG. 9B is now evaluated. Each query has two joins andfor this example, both join operations are evaluated in a pipeline. Foreach complex query one can evaluate the query by using onlyancestor-based join algorithms or using only descendant-based joinalgorithms. These two approaches are labeled with suffixes “-A2” and“-D2” for the ancestor-based and descendant-based approaches,respectively.

The performance comparison of the STJ and TMJ algorithms for both queryevaluation approaches (A2 and D2) is shown in FIG. 11. From the figure,it can be seen that STJ-D2 has the highest performance once again, sinceit is never has to spool nodes to intermediate files.

The present invention provides novel join algorithms for dealing with acore operation central to much of XML query processing, both for nativeXML query processor implementations as well for relational XML queryprocessors. In particular, the Stack-Tree family of structural joinalgorithms is both I/O and CPU optimal, and practically efficient.

One skilled in the art will appreciate further features and advantagesof the invention based on the above-described embodiments. Accordingly,the invention is not to be limited by what has been particularly shownand described, except as indicated by the appended claims. Allpublications and references cited herein are expressly incorporatedherein by reference in their entirety.

1. A method of query pattern matching, comprising: (a) generating a listof potential ancestors and a list of potential descendants; (b) sortingthe list of potential ancestors and the list of potential descendants inorder of a first attribute in a database; (c) skipping over unmatchablenodes in the list of potential descendants; (d) determining whether asecond attribute of a current node in the potential descendant list isless than a second attribute of a current node in the potential ancestorlist; (e) determining, based upon a result from (d), whether a firstattribute of the current node of the potential ancestor list is lessthan a first attribute of the current node of the potential descendantlist, a second attribute of the current node of the potential descendantlist is less than a second attribute of the current node of thepotential ancestor list, and a level number of the current node of thepotential descendant list is equal to a level number plus one of thecurrent node of the potential ancestor list; and (f) appending to anoutput join list, based upon a result from (e), a node pair comprisingthe current node of the potential ancestor list and the current node ofthe potential descendant list.
 2. The method according to claim 1,wherein the first attribute corresponds to a start position.
 3. Themethod according to claim 1, wherein the second attribute corresponds toan end position.
 4. The method according to claim 1, further includingmatching the query pattern against an Extensible Markup Language (XML)database.
 5. The method according to claim 1, further including sortingthe output join list in ancestor/parent order.
 6. A method of querypattern matching, comprising: (a) generating a list of potentialancestors and a list of potential descendants; (b) sorting the list ofpotential ancestors and the list of potential descendants in order of astart position attribute in a database; (c) skipping over unmatchablenodes in the list of potential ancestors; (d) determining whether astart position of a current node in the potential ancestor list is lessthan a start position of a current node in the potential descendantlist; (e) determining, based upon a result from (d), whether a startposition of the current node of the potential ancestor list is less thana start position of the current node of the list of potentialdescendants, an end position of the current node of the potentialdescendant list is less than an end position of the current node of thepotential ancestor list, and a level number of the current node of thepotential descendant list is equal to a level number plus one of thecurrent node of the potential ancestor list; and (f) appending to anoutput join list, based upon a result from (e), a node pair comprisingthe current node of the potential ancestor list and the current node ofthe potential descendant list.
 7. The method according to claim 6,further including matching the query pattern in an Extensible MarkupLanguage (XML) document.
 8. The method according to claim 6, furtherincluding sorting the output join list in descendant/child order.